home *** CD-ROM | disk | FTP | other *** search
Java Source | 2004-03-20 | 13.9 KB | 371 lines |
- /*
- * Copyright (C) 2003 Bob Tantlinger
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- */
-
- import java.io.*;
- import java.text.*;
- import java.util.*;
- //import java.sql.*;
-
- public class BlogPageWriter extends PrintWriter
- {
- public static int CAT_OR_ARC = -1;
- public static int INDEX = -2;
- public static int FRONT_PAGE = -3;
- private int pageType;
-
- //Tags that work anywhere
- public static final String BLOG_URL = "<$FrontPageLink$>";
- public static final String RSS_FILE = "<$RssLink$>";
- public static final String BLOG_TITLE = "<$BlogTitle$>";
- public static final String BLOG_DESC = "<$BlogDescription$>";
- public static final String ARC_INDEX = "<$IndexPageLink$>";
- public static final String PAGE_TITLE = "<$PageTitle$>"; //unique
-
- public static final String ARCS_BEGIN = "<ArchiveList>";
- public static final String ARC_LINK = "<$ArchiveLink$>";
- public static final String ARC_NAME = "<$ArchiveName$>";
- public static final String ARCS_END = "</ArchiveList>";
-
- //unique to Thingamablog
- public static final String CATS_BEGIN = "<CategoryList>";
- public static final String CAT_LINK = "<$CategoryLink$>";
- public static final String CAT_NAME = "<$CategoryName$>";
- public static final String CATS_END = "</CategoryList>";
-
- //Tags that only have meaning between <Blogger> and </Blogger>
- //This would be an entry template
- public static final String BLOG_ENTRIES_BEGIN = "<BlogEntry>";
-
- public static final String DAY_HEADER_BEGIN = "<DayHeader>";
- public static final String DAY_HEADER_DATE = "<$DayHeaderDate$>";
- public static final String DAY_HEADER_END = "</DayHeader>";
- public static final String DAY_FOOTER_BEGIN = "<DayFooter>";
- public static final String DAY_FOOTER_END = "</DayFooter>";
-
- public static final String ENTRY_BODY = "<$EntryBody$>";
- public static final String ENTRY_ID = "<$EntryID$>";
- public static final String ENTRY_AUTHOR = "<$EntryAuthor$>";
- public static final String ENTRY_AUTHOR_EMAIL = "<$EntryAuthorEmail$>";
- public static final String ENTRY_AUTHOR_URL = "<$EntryAuthorURL$>";
- public static final String ENTRY_DATE = "<$EntryDate$>";//unique
- public static final String ENTRY_TIME = "<$EntryTime$>";//unique
- public static final String ENTRY_DATE_TIME = "<$EntryDateTime$>";
- public static final String ENTRY_ARCPAGE = "<$EntryArchivePage$>";
-
- public static final String ENTRY_TITLE_BEGIN = "<EntryTitle>";
- public static final String ENTRY_TITLE = "<$EntryTitle$>";
- public static final String ENTRY_TITLE_END = "</EntryTitle>";
-
- public static final String ENTRY_CATS_BEGIN = "<EntryCategories>";//unique
- public static final String ENTRY_CATS_END = "</EntryCategories>";//unique
-
- //unique
- public static final String ENTRY_MODIFIED_BEGIN = "<EntryModifiedDate>";
- public static final String ENTRY_MODIFIED_DATE = "<$EntryModifiedDate$>";
- public static final String ENTRY_MODIFIED_END = "</EntryModifiedDate>";
-
-
- public static final String BLOG_ENTRIES_END = "</BlogEntry>";
-
- public static synchronized String convertBloggerTemplate(String tmpl)
- {
- tmpl = replaceVariable("<$BlogURL$>", BLOG_URL, tmpl);
- tmpl = replaceVariable("<$BlogTitle$>", BLOG_TITLE, tmpl);
- tmpl = replaceVariable("<$BlogDescription$>", BLOG_DESC, tmpl);
- tmpl = replaceVariable("<$BlogArchiveFileName$>", ARC_INDEX, tmpl);
- tmpl = replaceVariable("<BloggerArchives>", ARCS_BEGIN, tmpl);
- tmpl = replaceVariable("<$BlogArchiveLink$>", ARC_LINK, tmpl);
- tmpl = replaceVariable("<$BlogArchiveName$>", ARC_NAME, tmpl);
- tmpl = replaceVariable("</BloggerArchives>", ARCS_END, tmpl);
-
- tmpl = replaceVariable("<Blogger>", BLOG_ENTRIES_BEGIN, tmpl);
- tmpl = replaceVariable("<BlogDateHeader>", DAY_HEADER_BEGIN, tmpl);
- tmpl = replaceVariable("<$BlogDateHeaderDate$>", DAY_HEADER_DATE, tmpl);
- tmpl = replaceVariable("</BlogDateHeader>", DAY_HEADER_END, tmpl);
- tmpl = replaceVariable("<BlogDateFooter>", DAY_FOOTER_BEGIN, tmpl);
- tmpl = replaceVariable("</BlogDateFooter>", DAY_FOOTER_END, tmpl);
- tmpl = replaceVariable("<$BlogItemBody$>", ENTRY_BODY, tmpl);
- tmpl = replaceVariable("<$BlogItemNumber$>", ENTRY_ID, tmpl);
- tmpl = replaceVariable("<$BlogItemAuthor$>", ENTRY_AUTHOR, tmpl);
- tmpl = replaceVariable("<$BlogItemAuthorEmail$>", ENTRY_AUTHOR_EMAIL, tmpl);
- tmpl = replaceVariable("<$BlogItemAuthorURL$>", ENTRY_AUTHOR_URL, tmpl);
- tmpl = replaceVariable("<$BlogItemDateTime$>", ENTRY_DATE_TIME, tmpl);
- tmpl = replaceVariable("<$BlogItemArchiveFileName$>", ENTRY_ARCPAGE, tmpl);
- tmpl = replaceVariable("<BlogItemTitle>", ENTRY_TITLE_BEGIN, tmpl);
- tmpl = replaceVariable("<$BlogItemTitle$>", ENTRY_TITLE, tmpl);
- tmpl = replaceVariable("</BlogItemTitle>", ENTRY_TITLE_END, tmpl);
- tmpl = replaceVariable("</Blogger>", BLOG_ENTRIES_END, tmpl);
-
- return tmpl;
- }
-
- BlogVariables vals;
- String template;
- String catListTemplate, arcListTemplate, entryTemplate;
- String entryTitleTemplate, entryCatsTemplate, entryModifiedTemplate;
- String dayHeaderTemplate, dayFooterTemplate;
- String pageHeader = "", pageFooter = "";
- int counter;
- int day;
- File archiveDir;
-
- BlogEntry prevEntry;
- String prevArcPage, prevDayHeader, prevDayFooter;
-
- public BlogPageWriter(FileWriter fw, File arcDir,
- BlogVariables bv, String tmp, String title, int type)
- {
- super(fw);
- pageType = type;
- archiveDir = arcDir;
- vals = bv;
- template = tmp;
- template = replaceVariable(BLOG_TITLE, vals.getTitle(), template);
- template = replaceVariable(BLOG_DESC, vals.getDescription(), template);
- template = replaceVariable(BLOG_URL,
- vals.getBaseUrl() + vals.getMainPageFileName(), template);
- template = replaceVariable(ARC_INDEX,
- vals.getBaseUrl() + vals.getIndexPageFileName(), template);
- template = replaceVariable(RSS_FILE,
- vals.getBaseUrl() + vals.getRssFileName(), template);
- template = replaceVariable(PAGE_TITLE, title, template);
-
- //write the lists
- while(template.indexOf(CATS_BEGIN) > 0 && template.indexOf(CATS_END) > 0)
- {
- catListTemplate = parseSubTemplate(CATS_BEGIN, CATS_END, template);
- String catList = buildCatList(vals.getCategories(), catListTemplate);
- template = replaceVariable(
- CATS_BEGIN + catListTemplate + CATS_END, catList, template);
- }
-
- while(template.indexOf(ARCS_BEGIN) > 0 && template.indexOf(ARCS_END) > 0)
- {
- arcListTemplate = parseSubTemplate(ARCS_BEGIN, ARCS_END, template);
- String arcList = buildArcList(arcListTemplate);
- template = replaceVariable(
- ARCS_BEGIN + arcListTemplate + ARCS_END, arcList, template);
- }
-
- if(pageType == INDEX)
- return;
-
- entryTemplate = parseSubTemplate(BLOG_ENTRIES_BEGIN, BLOG_ENTRIES_END, template);
- dayHeaderTemplate = parseSubTemplate(DAY_HEADER_BEGIN, DAY_HEADER_END, entryTemplate);
- dayFooterTemplate = parseSubTemplate(DAY_FOOTER_BEGIN, DAY_FOOTER_END, entryTemplate);
- entryTitleTemplate = parseSubTemplate(ENTRY_TITLE_BEGIN, ENTRY_TITLE_END, entryTemplate);
- entryCatsTemplate = parseSubTemplate(ENTRY_CATS_BEGIN, ENTRY_CATS_END, entryTemplate);
- entryModifiedTemplate = parseSubTemplate(ENTRY_MODIFIED_BEGIN, ENTRY_MODIFIED_END, entryTemplate);
-
- counter = 0;
- }
-
- public void writeEntry(BlogEntry be, String arcPage) throws IOException
- {
- if(pageType == INDEX)
- return;
-
- if(counter == 0)
- {
- if((template.indexOf(BLOG_ENTRIES_BEGIN) < 0) ||
- (template.indexOf(BLOG_ENTRIES_END) < 0))
- return;
- pageHeader = template.substring(0, template.indexOf(BLOG_ENTRIES_BEGIN));
- pageFooter = template.substring(
- template.indexOf(BLOG_ENTRIES_END) + BLOG_ENTRIES_END.length(),
- template.length() - 1);
- println(pageHeader);
- }
-
- String dFooter = "", dHeader = "";
- Calendar cal = Calendar.getInstance();
- cal.setTime(new Date(be.getHeaderData().getTimestamp().getTime()));
- boolean dayChanged = false;
- if(cal.get(Calendar.DAY_OF_YEAR) != day)
- {
- if(counter > 0)
- dFooter = dayFooterTemplate;
- dHeader = replaceVariable(
- DAY_HEADER_DATE, vals.getDateFormat().format(be.getTimestamp()),
- dayHeaderTemplate);
- day = cal.get(Calendar.DAY_OF_YEAR);
- dayChanged = true;
- }
-
- if(counter > 0)
- {
- if(dayChanged)
- {
- writeOut(prevEntry, prevArcPage, prevDayHeader, dFooter);
- }
- else
- writeOut(prevEntry, prevArcPage, prevDayHeader, "");
- }
-
- counter++;
- prevEntry = be;
- prevArcPage = arcPage;
- prevDayHeader = dHeader;
- prevDayFooter = dFooter;
- }
-
- protected void writeOut(BlogEntry be, String arcPage, String dHeader, String dFooter) throws IOException
- {
- SimpleDateFormat tf = vals.getTimeFormat();
- SimpleDateFormat df = vals.getDateFormat();
- String time = tf.format(be.getTimestamp());
- String date = df.format(be.getTimestamp());
- String dateTime = date + ' ' + time;
- Author author = vals.getAuthor(be.getAuthor());
- if(author == null)author = new Author();
-
- String eTmp = new String(entryTemplate);
-
- String entryTitle = "";
- if(be.getTitle() != null || !be.getTitle().equals(""))
- entryTitle = replaceVariable(ENTRY_TITLE, be.getTitle(), entryTitleTemplate);
-
-
- String modDate = "";
- if(be.getLastModified() != null)
- modDate = replaceVariable(ENTRY_MODIFIED_DATE,
- df.format(be.getLastModified()) + " " + tf.format(be.getLastModified()),
- entryModifiedTemplate);
-
- String catList = buildCatList(be.getCategories(), entryCatsTemplate);
-
- eTmp = replaceVariable(ENTRY_CATS_BEGIN + entryCatsTemplate + ENTRY_CATS_END, catList, eTmp);
- eTmp = replaceVariable(ENTRY_TITLE_BEGIN + entryTitleTemplate + ENTRY_TITLE_END, entryTitle, eTmp);
- eTmp = replaceVariable(DAY_FOOTER_BEGIN + dayFooterTemplate + DAY_FOOTER_END, dFooter, eTmp);
- eTmp = replaceVariable(DAY_HEADER_BEGIN + dayHeaderTemplate + DAY_HEADER_END, dHeader, eTmp);
- eTmp = replaceVariable(ENTRY_DATE, date, eTmp);
- eTmp = replaceVariable(ENTRY_TIME, time, eTmp);
- eTmp = replaceVariable(ENTRY_DATE_TIME, dateTime, eTmp);
- eTmp = replaceVariable(ENTRY_MODIFIED_BEGIN + entryModifiedTemplate + ENTRY_MODIFIED_END, modDate, eTmp);
- eTmp = replaceVariable(ENTRY_BODY, be.getEntryText(), eTmp);
- eTmp = replaceVariable(ENTRY_AUTHOR, author.getName(), eTmp);
- eTmp = replaceVariable(ENTRY_AUTHOR_EMAIL, author.getEmailAddress(), eTmp);
- eTmp = replaceVariable(ENTRY_AUTHOR_URL, author.getUrl(), eTmp);
- eTmp = replaceVariable(ENTRY_ID, be.getID() + "", eTmp);
-
- if(pageType == FRONT_PAGE)
- arcPage = (vals.getArchivesUrl().endsWith("/") ?
- vals.getArchivesUrl() :
- vals.getArchivesUrl() + '/') + arcPage;
-
- eTmp = replaceVariable(ENTRY_ARCPAGE, arcPage, eTmp);
-
- println(eTmp + "\n");
- }
-
- public void closePage() throws IOException
- {
- if(pageType == INDEX)
- println(template);
- else
- {
- if(counter > 0)
- {
- writeOut(prevEntry, prevArcPage, prevDayHeader, dayFooterTemplate);
- println(pageFooter);
- }
- else
- println(replaceVariable(entryTemplate, "No Entries", template));
- }
- }
-
- protected String parseSubTemplate(String openTag, String closeTag, String tmpl)
- {
- StringBuffer sb = new StringBuffer(tmpl);
- int s = sb.toString().indexOf(openTag);
- if(s < 0)
- return "";
-
- int e = s + openTag.length();
- int tempStart = e;
-
- int tempEnd = sb.toString().indexOf(closeTag);
- if(tempEnd < tempStart)
- return "";
-
- return sb.toString().substring(tempStart, tempEnd);
- }
-
- protected String buildCatList(String cats[], String tmpl)
- {
- String list = "";
- if(cats == null || cats.length == 0)
- return list;
- String arcUrl = vals.getArchivesUrl();
- if(!arcUrl.endsWith("/"))
- arcUrl += "/";
-
- for(int i = 0; i < cats.length; i++)
- {
- CategoryPage cp = new CategoryPage(archiveDir.getAbsolutePath(), cats[i]);
- String temp = tmpl;
- temp = replaceVariable(CAT_LINK, arcUrl + cp.getName(), temp);
- temp = replaceVariable(CAT_NAME, cats[i], temp);
- list += temp;
- }
-
- return list;
- }
-
- protected String buildArcList(String tmpl)
- {
- String list = "";
- DatedArchivePage daps[] = DatedArchivePage.listDatedArchivePages(archiveDir);
- if(daps == null || daps.length == 0)
- return list;
- String arcUrl = vals.getArchivesUrl();
- if(!arcUrl.endsWith("/"))
- arcUrl += "/";
-
- for(int i = 0; i < daps.length; i++)
- {
- String temp = tmpl;
- temp = replaceVariable(ARC_LINK, arcUrl + daps[i].getName(), temp);
- temp = replaceVariable(ARC_NAME, daps[i].getTextString(), temp);
- list += temp;
- }
-
- return list;
- }
-
- protected static String replaceVariable(String var, String val, String tmpl)
- {
- if(var.equals(""))//don't try to process empty templates
- return tmpl;
-
- while(tmpl.indexOf(var) != -1 && !var.equals(val))
- {
- StringBuffer sb = new StringBuffer(tmpl);
- int s = sb.toString().indexOf(var);
- int e = s + var.length();
- sb.delete(s, e);
- sb.insert(s, val);
- tmpl = sb.toString();
-
- //System.out.println("Replaced " + var + " with " + val);
- }
-
- return tmpl;
- }
- }